<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/defaultconf.mex");
require_once($_SERVER['DOCUMENT_ROOT'] . "/Modules/FTK9D4D0/modconf.mex");
MYTCONN();
//==========================Vars===========================
$TimeStart = $_POST['TStart'];
$TimeEnd = $_POST['TEnd'];
$cb_Banco = 0;
$Country = null;
$Origen0 = null;
$QOrigen0 = null;
$Origen1 = null;
$QOrigen1 = null;
$QCorresponsal = null;
$Status = null;
$QStatus = null;

$OrderAD1 = 'ASC';
$OrderAD2 = 'ASC';
$OrderAD3 = 'ASC';

$OrderBy1 = '`FraID` ' . $OrderAD1;
$OrderBy2 = null;
$OrderBy3 = null;

if (isset($_POST['Country']) && $_POST['Country'] != null) {
    $Country = ' AND `FraBnfPais` = ' . $_POST['Country'];
}
if (isset($_POST['cb_Banco']) && $_POST['cb_Banco'] != null) {
    $cb_Banco = $_POST['cb_Banco'];
}
if (isset($_POST['FraOrigen0']) && $_POST['FraOrigen0'] != null) {
    $Origen0 = $_POST['FraOrigen0'];
    $QOrigen0 = ' AND `FraOrigen0` IN (SELECT `OriNumero` FROM `origenes` WHERE `OriDominio` = ' . $Origen0 . ')';
}
if (isset($_POST['FraOrigen1']) && $_POST['FraOrigen1'] != null) {
    $Origen1 = $_POST['FraOrigen1'];
    $QOrigen1 = ' AND `FraOrigen1` = ' . $Origen1;
}
if ($_POST['FraCorresponsal'] > 0) {
    $QCorresponsal = " AND `FraCorresponsal` = " . $_POST['FraCorresponsal'];
}
if (isset($_POST['FraStatus']) && $_POST['FraStatus'] != null) {
    $Status = $_POST['FraStatus'];
    $QStatus = ' AND `FraStatus` = ' . $Status;
}
if (isset($_POST['FraModo']) && $_POST['FraModo'] != null) {
    $Modo = $_POST['FraModo'];
    $QModo = ' AND `FraModo` = ' . $Modo;
}

if ($_POST['OrderBy1'] != null) {
    $OrderBy1 = '`' . $_POST['OrderBy1'] . '` ' . $_POST['OrderAD1'];
}
if ($_POST['OrderBy2'] != null) {
    $OrderBy2 = ',`' . $_POST['OrderBy2'] . '` ' . $_POST['OrderAD2'];
}
if ($_POST['OrderBy3'] != null) {
    $OrderBy3 = ',`' . $_POST['OrderBy3'] . '` ' . $_POST['OrderAD3'];
}
if (isset($_POST['VIEWColumns'])) {
    $Colums = $_POST['VIEWColumns'];
    $CCols = count($_POST['VIEWColumns']);
}
if (isset($Colums)) {

    $QColums = '';
    for ($x = 0; $x < $CCols; $x++) {
        if ($Colums[$x] == 'ORAccount') {
            $tr = $x;
            $QColums .= '';
        } else {
            if ($Colums[$x] == 'FraStatus') {
                $QColums .= ',`sg_Abreviatura` AS FraStatus';
            } elseif ($Colums[$x] == 'FraCorresponsal') {
                $QColums .= ',`CorrAlias` AS FraCorresponsal';
            } elseif ($Colums[$x] == 'FraModoPago') {
                $QColums .= ',`mp_Short` AS FraModoPago';
            } else {
                $QColums .= ',`' . $Colums[$x] . '`';
            }
        }
    }
    if (isset($_POST['BBar_Status'])) {
        $QColums .= ',`FraCargo`';
    }
    $cmStr = '';
    $dsStr = '';
    for ($x = 0; $x < $CCols; $x++) {
        $dsStr .= ',{name: \'' . $Colums[$x] . '\', type:\'string\'}';
    }

    for ($x = 0; $x < $CCols; $x++) {
        $cmStr .= ',{header: "' . $Colums[$x] . '", width: 80, sortable: true, dataIndex: \'' . $Colums[$x] . '\'}';
    }
    if ($_POST['FraModo'] == 0 || $_POST['FraModo'] == 1) {
        $GETQUR = "SELECT FraModo, FraIciIncidencia, `FraID`, `FraIciCodeField`, `FraIciOldValue`, `FraIciNewValue`,`FraFechaIngreso`, `FraEnviado`,`FraEnviadoUSD`,`FraARecibir`,`FraCorresponsal`
  					{$QColums}
  				FROM `facturas`
                       INNER JOIN facturas_incidencias ON FraID = FraIciID
			LEFT  OUTER JOIN `facturasextjoins` ON `FraID` = `FraExtID`
			LEFT OUTER JOIN `beneficiarioscuentas` ON cb_Id = `FraCtaBenef`
			LEFT OUTER JOIN `hf_actypes` ON `hf_actypes`.`REF` = `cb_TipoCuenta`
			LEFT OUTER JOIN `hd_statusfacturas` ON `sg_ID` = `FraStatus`
			LEFT OUTER JOIN `corresponsales` ON `CorrNumero` = `FraCorresponsal`
			LEFT OUTER JOIN `hd_bancos` ON `bp_ID` = `cb_Banco`
			LEFT OUTER JOIN `hd_modospago` ON `mp_ID` = `FraModoPago`
			WHERE DATE(`FraIciDate`) BETWEEN '{$TimeStart}' AND '{$TimeEnd}'
			{$QOrigen0}{$QOrigen1}{$QCorresponsal}{$Country}{$QStatus} AND FraModo = 1";
    }
    if ($_POST['FraModo'] == 0 || $_POST['FraModo'] == 2) {

        if ($_POST['FraModo'] == 0) {
            $GETQUR.=' UNION ';
        } else {
            $GETQUR = "";
        }

        $GETQUR .= "SELECT FraModo, FraIciIncidencia, `FraID`, `FraIciCodeField`, `FraIciOldValue`, `FraIciNewValue`,`FraFechaIngreso`, `FraEnviado`,`FraEnviadoUSD`,`FraARecibir`,`FraCorresponsal`
  					{$QColums}
  				FROM `facturas`
			INNER JOIN facturas_incidencias ON FraID = FraIciID
			LEFT OUTER JOIN `facturas_income` ON `FraID` = `FraIncID`
			LEFT OUTER JOIN `facturasextjoins` ON `FraID` = `FraExtID`
			LEFT OUTER JOIN `beneficiarioscuentas` ON cb_Id = `FraCtaBenef`
			LEFT OUTER JOIN `hf_actypes` ON `hf_actypes`.`REF` = `cb_TipoCuenta`
			LEFT OUTER JOIN `hd_statusfacturas` ON `sg_ID` = `FraStatus`
			LEFT OUTER JOIN `corresponsales` ON `CorrNumero` = `FraIncCorresponsal`
			LEFT OUTER JOIN `hd_bancos` ON `bp_ID` = `cb_Banco`
			LEFT OUTER JOIN `hd_modospago` ON `mp_ID` = `FraModoPago`
			WHERE DATE(`FraIciDate`) BETWEEN '{$TimeStart}' AND '{$TimeEnd}'
			{$QOrigen0}{$QOrigen1}{$QCorresponsal}{$Country}{$QStatus} AND FraModo = 2";
    }
    $GETQUR .= " ORDER BY {$OrderBy1}{$OrderBy2}{$OrderBy3}";

    $GET = $GLOBALS['MYT']->db_array($GETQUR);
    $CGET = $GET[0];

    $sums['EUR'] = 0;
    $sums['USD'] = 0;
    $sums['BRL'] = 0;
    $rsStr = '';

    for ($i = 1; $i <= $CGET; $i++) {
        $sums['EUR'] = $sums['EUR'] + $GET[$i]['FraEnviado'];
        $sums['USD'] = $sums['USD'] + $GET[$i]['FraEnviadoUSD'];
        $sums['BRL'] = $sums['BRL'] + $GET[$i]['FraARecibir'];
        if (isset($_POST['BBar_Status'])) {
            $sums['EUR'] = $sums['EUR'] + $GET[$i]['FraCargo'];
        }
        $rsStr .= '[' . $i . ',\'' . $__sysFraModo[$GET[$i]['FraModo']] . '\',' . $GET[$i]['FraIciIncidencia'] . ',' . $GET[$i]['FraID'] . ',\'' . $GET[$i]['FraIciCodeField'] . '\',\'' . $GET[$i]['FraIciOldValue'] . '\',\'' . $GET[$i]['FraIciNewValue'] . '\',\'' . $GET[$i]['FraFechaIngreso'] . '\',' . $GET[$i]['FraEnviado'] . ',' . $GET[$i]['FraEnviadoUSD'] . ',' . $GET[$i]['FraARecibir'];
        for ($x = 0; $x < $CCols; $x++) {
            $ColumnX = $Colums[$x];
            if (isset($GET[$i]['FraTasa']) && $GET[$i][$ColumnX] == $GET[$i]['FraTasa']) {
                $GET[$i][$ColumnX] = number_format($GET[$i][$ColumnX], 4);
            }
            $rsStr .= ',\'' . $GET[$i][$ColumnX] . '\'';
        }
        $rsStr .= ']';
        if ($i < $CGET) {
            $rsStr .= ',';
        }
    }
?>
    <script charset="utf-8" type="text/javascript">
        //disableSelection(document.getElementById("ICIList"))
        Ext.onReady(function(){
            $elem = 262;
            windowHeight = 0;
            if( typeof( window.innerWidth ) == 'number' ) {
                windowHeight = window.innerHeight-$elem;
            } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                windowHeight = document.documentElement.clientHeight-$elem;
            }
            var myData = [<?php echo $rsStr; ?>];

            var ds = new Ext.data.Store({
                reader: new Ext.data.ArrayReader({}, [
                    {name: 'id'},
                    {name: 'CnlID', type: 'string'},
                    {name: 'IciID', type: 'int'},
                    {name: 'FraID', type: 'int'},
                    {name: 'IciField', type: 'string'},
                    {name: 'IciNew', type: 'string'},
                    {name: 'IciOld', type: 'string'},
                    {name: 'Data', type: 'date', dateFormat: 'Y-m-d H:i:s'},
                    {name: 'mon_1', type: 'float'},
                    {name: 'mon_2', type: 'float'},
                    {name: 'mon_3', type: 'float'}
<?php echo $dsStr; ?>
                                   ])
                               });
                               ds.loadData(myData);
                               var colModel = new Ext.grid.ColumnModel([
                                   {header: "Canal", width: 60, sortable: true, locked:true, dataIndex: 'CnlID'},
                                   {header: "Incidencia", width: 60, sortable: true, locked:true, dataIndex: 'IciID'},
                                   {header: "Numero", width: 60, sortable: true, locked:true, dataIndex: 'FraID'},
                                   {header: "Campo", width: 100, sortable: true, locked:true, dataIndex: 'IciField'},
                                   {header: "Novo Valor", width: 150, sortable: true, locked:true, dataIndex: 'IciNew'},
                                   {header: "Valor a Corrigir", width: 150, sortable: true, locked:true, dataIndex: 'IciOld'},
                                   {header: "Data", width: 150, sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'), dataIndex: 'Data'},
                                   {header: "Enviado", width: 75, sortable: true, locked:true, align: 'right', dataIndex: 'mon_1'},
                                   {header: "Env. Relacao", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'mon_2'},
                                   {header: "A pagar", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'mon_3'}
<?php echo $cmStr; ?>
                            ]);
                            Bars = new Ext.Toolbar({
                                store: this.store,
                                items : [ {
                                        text: "<?php echo $CGET; ?> Registos | EUR: <?php echo number_format($sums['EUR'], 2); ?> | USD: <?php echo number_format($sums['USD'], 2); ?> | BRL: <?php echo number_format($sums['BRL'], 2); ?>"
                                    },{
                                        type:'button',
                                        loadMask: true,
                                        text:'Sem Accao'
                                        /*,
                    handler:function(){
                        var ss = '';
                        var m = selmode.getSelections();
                        for(var i = 0, len = m.length; i < len; i++){
                            ss += m[i].get("FraID") + ",";}
                        document.FMORDNGEN.POOL.value = ss;
                        document.FMORDNGEN.action='Modules/FTK9D4D0/Lib/a05_vie_console.mex';
                        subform('FMORDNGEN','SUMS');
                        updateCorr();
                    }*/
                                    }]
                            });

                            var selmode = new Ext.grid.RowSelectionModel({});
                            var Console = new Ext.grid.GridPanel({
                                height: windowHeight-35,
                                ds: ds,
                                cm: colModel,
                                sm: selmode,
                                listeners: {
                                    rowdblclick: function(ds, row) {
                                        var record = ds.getStore().getAt(row);
                                        var value = record.get('FraID');
                                        menuwindow('Operativo','_response/call.php?get=Operative/pp_frm&OR='+value,800,520,'MnMain');
                                    }
                                },
                                bbar: Bars,
                                renderTo: 'ICIList'
                            });
                        });
    </script>
<?php } ?>
